package action;

import java.util.*;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import db.*;

@SuppressWarnings("serial")
public class IndexAction extends ActionSupport{
	private User user;
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@SuppressWarnings("unchecked")
	public String execute(){
		Map session = ActionContext.getContext().getSession();
		String session_userId = (String)session.get("userId");
		String userRole = (String)session.get("userRole");
		String userfullname = (String)session.get("userfullname");
 		if (null == session_userId|| null == userRole||null == userfullname)
			return ERROR;
		Integer userId = 0;
		try{
			userId = Integer.parseInt(session_userId);
		}catch(Exception e){
			System.out.println("Fake session value detected.");
			//e.printStackTrace();
		}
		UserDAO userDao = new UserDAO();
		user = userDao.findById(userId);
		if (user == null)
			return ERROR;
		if (checkAdmin())
			return "admin";
		else return "user";
		//return ERROR;
	}
	private boolean checkAdmin(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		if( new String("1").equals((String)(session.getAttribute("userRole"))) )
			return true;
		return false;
	}
}
